#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "Motor.h"
#include "Serial.h"
#include "Sonic.h"

uint8_t speed = 40;
uint8_t RxData;
float dis;

int main(void)
{
	OLED_Init();
	Motor_Init();
	Serial_Init();
	Sonic_Init();
	//初始速度为100
	PWM_SetCompare(speed);
	OLED_ShowString(1, 1, "start");
	OLED_ShowString(2, 1, "Speed:");
	OLED_ShowNum(2, 7, speed, 3);
	while(1)
	{
		int int_length,decimal_lenght;
		dis = Sonic_Distance();
		int_length = (uint16_t)dis;//小数点前的数字
		decimal_lenght = dis * 1000 - int_length * 1000;//小数点后的数字

		OLED_ShowNum(3, 1, int_length, 2);
		OLED_ShowString(3, 3, ".");
		OLED_ShowNum(3, 4, decimal_lenght, 3);
		if(int_length > 30)
			{
				Run();
			}
			if(int_length <= 30)
			{
				Back();
			}
			if(int_length <= 7)
			{
				Left();
				Delay_ms(100);
				Stop();
			}
	}
}